import { createRouter, createWebHashHistory } from 'vue-router'
import Cookie from 'js-cookie'
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: () => import('@/views/login/index.vue'),
    }, {
      path: '/Layout',
      name: 'Layout',
      component: () => import('@/components/Layout/container/index.vue'),
      children: [
        {
          path: '',
          name: 'Home',
          component: () => import('@/views/home/index.vue'),
        }, {
          path: '/enterprise',
          name: 'enterprise',
          component: () => import('@/views/enterprise/index.vue'),
        }, {
          path: '/shop',
          name: 'shop',
          component: () => import('@/views/shop/index.vue'),
        }, {
          path: '/news',
          name: 'news',
          component: () => import('@/views/news/index.vue'),
        }, {
          path: '/product',
          name: 'product',
          component: () => import('@/views/product/index.vue'),
        },
        {
          path: '/contactUs',
          name: 'contactUs',
          component: () => import('@/views/contactUs/index.vue'),
        }, {
          path: '/users',
          name: 'users',
          component: () => import('@/views/users/index.vue'),
        }, {
          path: '/caseAdd',
          name: 'caseAdd',
          component: () => import('@/views/caseAdd/index.vue'),
        }, {
          path: '/usersOff',
          name: 'usersOff',
          component: () => import('@/views/usersOff/index.vue'),
        }, {
          path: '/store',
          name: 'store',
          component: () => import('@/views/store/index.vue'),
        }, {
          path: '/qualityMan',
          name: 'qualityMan',
          component: () => import('@/views/qualityMan/index.vue'),
        }, {
          path: '/filmAdd',
          name: 'filmAdd',
          component: () => import('@/views/filmAdd/index.vue'),
        }, {
          path: '/WarrantySerach',
          name: 'WarrantySerach',
          component: () => import('@/views/WarrantySerach/index.vue'),
        }, {
          path: '/carAdd',
          name: 'carAdd',
          component: () => import('@/views/carAdd/index.vue'),
        }, {
          path: '/number',
          name: 'number',
          component: () => import('@/views/number/index.vue'),
        }
      ]
    },
  ],
})
router.beforeEach((to, from) => {
  let token = Cookie.get('token')
  if (
    // 检查用户是否已登录
    !token &&
    // ❗️ 避免无限重定向
    to.name !== 'login'
  ) {
    // 将用户重定向到登录页面
    return { name: 'login' }
  } else if (token && to.name == 'login') {
    return { name: 'Layout' }
  }
})
export default router
